<?php
function login()
{
    if (empty($_POST['username'])) {
        $arr = array("message" => "请输入账号", "code" => 0);
        print_r(json_encode($arr));
        return;
    }
    if (empty($_POST['password'])) {
        $arr = array("message" => "请输入密码", "code" => 0);
        print_r(json_encode($arr));
        return;
    }
    $uname = $_POST['username'];
    $pword = $_POST['password'];

    $link = mysqli_connect('127.0.0.1', 'root', 'root', 'demo1');

    $res = mysqli_query($link, "SELECT * FROM `users` WHERE `username` = '$uname' AND `password` = '$pword'");

    $row = mysqli_fetch_assoc($res);

    mysqli_close($link);
    if ($row) {
        $arr = array("message" => "登录成功",
        "code" => 1,
        "username" => $uname,
        "password" => $pword
        );
    } else {
        $arr = array("message" => "用户名或者密码错误", "code" => 0);
    }
    // 把这关联型数组返回
    print_r(json_encode($arr));
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    login();
}
